package com.earnings.okhttputils.utils.car.ui;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.IdRes;
import android.view.View;
import android.webkit.WebSettings;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.UrlConstant.CarUrl;
import com.earnings.okhttputils.utils.bean.CarDetailsData;
import com.earnings.okhttputils.utils.car.BannerImageLoader.CarDetailsImageLoader;
import com.earnings.okhttputils.utils.car.http.CarHttp;
import com.earnings.okhttputils.utils.car.http.GodHttpDataCallback;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.view.AutoSizeWebView;
import com.youth.banner.Banner;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/15 0015.
 */

public class CarDetailsActivity extends GodLeftHandBaseActivity implements GodOnClickListener, RadioGroup.OnCheckedChangeListener {
    private Banner banner_view;
    private CarDetailsImageLoader mCarDetailsImageLoader;
    private RadioGroup radioGroup_back, radioGroup_front;
    private AutoSizeWebView autoSizeWebView;
    private SelectDialog mSelectDialog;

    @Override
    protected int onCreateLayoutId() {
        return R.layout.activity_cardetails;
    }

    @Override
    protected void init() {
        setTitle("车辆详情");
        banner_view = getView(R.id.banner_view);
        radioGroup_back = getView(R.id.radioGroup_back);
        radioGroup_front = getView(R.id.radioGroup_front);
        autoSizeWebView = getView(R.id.autoSizeWebView);
        mSelectDialog = new SelectDialog(this);
        String catid = getIntent().getStringExtra("catid");
        String id = getIntent().getStringExtra("id");
        if (catid == null || id == null) {
            Toast.makeText(this, "网络异常", Toast.LENGTH_LONG).show();
            return;
        }
        mCarDetailsImageLoader = new CarDetailsImageLoader();
        banner_view.setImageLoader(mCarDetailsImageLoader);
        setOnClickListener(this, R.id.service_tv, R.id.pay_tv, R.id.select_view, R.id.jisuanqi_view);
        http(catid, id);

        radioGroup_back.setOnCheckedChangeListener(this);
        //缓存
        autoSizeWebView.getSettings().setJavaScriptEnabled(true);
        autoSizeWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
    }


    private void http(String catid, String id) {
        showProgress();
        Map map = new HashMap();
        map.put("catid", catid);
        map.put("id", id);
        CarHttp carHttp = new CarHttp("good", map);
        OkHttpUtils.get().url(CarUrl.CAR_URL).params(carHttp).build().execute(new GodHttpDataCallback<CarDetailsData>() {
            @Override
            public void onSuccess(CarDetailsData data, String msg, String response) {
                uiUiData(data);
                mCarDetailsImageLoader.start(banner_view, data);
                mSelectDialog.setData(data);
                dismissProgress();
            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
            }
        });
    }

    private CarDetailsData data;

    private void uiUiData(CarDetailsData data) {
        this.data = data;
        setText(R.id.name_tv, data.getTitle());
        setText(R.id.money_tv, "指导价：￥" + data.getGprice() + "万");
        setText(R.id.caibao_tv, "获得财宝" + data.getPrice() + "万");
        autoSizeWebView.loadUrl(data.getOne());
    }


    @Override
    public void OnClickListener(View view) {
        //客服
        if (view.getId() == R.id.service_tv) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + "4001052918"));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
        if (view.getId() == R.id.select_view) {
            mSelectDialog.show();
        }

        //支付
        if (view.getId() == R.id.pay_tv) Toast.makeText(this, "敬请期待", Toast.LENGTH_SHORT).show();
        //计算器
        if (view.getId() == R.id.jisuanqi_view) {
            mIntent.putExtra("price", data.getGprice());
            mIntent.putExtra("name", data.getTitle());
            skipActivity(CalculatorActivity.class);
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        if (checkedId == R.id.radioBtn_buycaption_back) {
            radioGroup_front.check(R.id.radioBtn_buycaption_front);
            autoSizeWebView.loadUrl(data.getOne());
        }
        if (checkedId == R.id.radioBtn_detail_back) {
            radioGroup_front.check(R.id.radioBtn_detail_front);
            autoSizeWebView.loadUrl(data.getTwo());
        }
        if (checkedId == R.id.radioBtn_params_back) {
            radioGroup_front.check(R.id.radioBtn_params_front);
            autoSizeWebView.loadUrl(data.getThr());
        }
        if (checkedId == R.id.radioBtn_q_and_a_back) {
            radioGroup_front.check(R.id.radioBtn_q_and_a_front);
            autoSizeWebView.loadUrl(data.getFro());
        }
    }
}
